import { defineStore } from 'pinia';
import { parseTime } from '@/utils/format';

const system = defineStore({
  id: 'system',
  state: () => {
    return {
      isH5: false,
      lang: {
        code: 2,
        countryCode: "+44",
        countryCodeStr: null,
        ifStatus: 1,
        language: "English",
        languageGq: "@/assets/united-kingdom.png",
        locale: "en-US",
        resource: "https://ugaming.s3.ap-southeast-1.amazonaws.com/language/2023/02/14/1d8e11af-5197-4f42-b237-46381204c36b.png",
      },
      langList: <any>[],
      darkTheme: false,
      themeAuto: false, // 自动暗黑模式
      systemTime: parseTime(new Date()),
      systemTimeInterval: <any>null,
      localTime: parseTime(new Date()),
      localTimeInterval: <any>null,
      channel: {}, // 渠道信息
    };
  },
  persist: true,
  // 等同于vuex的getter
  getters: {
    getIsH5: (state) => state.isH5,
    getLang: (state) => state.lang,
    getDarkTheme: (state) => state.darkTheme,
    getLangList: (state) => state.langList,
    getThemeAuto: (state) => state.themeAuto,
    getSystemTimeInterval: (state) => state.systemTimeInterval,
    getSystemTime: (state) => state.systemTime, // 系统时间
    getLocalTime: (state) => state.localTime, // 本地时间
    getChannel: (state) => state.channel,
  },
  // pinia 放弃了 mutations 只使用 actions
  actions: {
    // actions可以用async做成异步形式
    setLang(locale: any) {
      this.lang = locale;
    },
    setIsH5(isH5: boolean) {
      this.isH5 = isH5;
    },
    setDarkTheme(darkTheme: boolean) {
      this.darkTheme = darkTheme;
    },
    setLangList(list: Array<any>) {
      this.langList = list;
    },
    setThemeAuto(themeAuto: boolean) {
      this.themeAuto = themeAuto;
    },
    setSystemTime(time: number){
      clearInterval(this.systemTimeInterval);
      let t = Number(time);
      this.systemTimeInterval = setInterval(()=>{
        this.systemTime = parseTime(t+=1000);
      }, 1000);
    },
    setLocalTime(){
      clearInterval(this.localTimeInterval);
      this.localTimeInterval = setInterval(()=>{
        this.localTime = parseTime(new Date());
      }, 1000);
    },
    setChannel(channel: any){
      this.channel = channel;
    },
  },

});

export default system;
